package amartin47;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
 

public class Test 
{
	public static void main(String argv[]) {
		 
		  try {
	 
			File fXmlFile = new File("biblefile.xml");
			DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
			Document doc = dBuilder.parse(fXmlFile);
			doc.getDocumentElement().normalize();
	 
			System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
			NodeList nList = doc.getElementsByTagName("item");
			System.out.println("-----------------------");
	 
			List<BibleVerse> list = new ArrayList<BibleVerse>();
			
			for (int temp = 0; temp < nList.getLength(); temp++)
			{
				Node nNode = nList.item(temp);
				if (nNode.getNodeType() == Node.ELEMENT_NODE)
				{  
					BibleVerse verse = new BibleVerse();
				    Element eElement = (Element) nNode;
				 
				    String book = getTagValue("bookname", eElement);
				    int chapter = Integer.parseInt(getTagValue("chapter", eElement));
				    int verseNumber = Integer.parseInt(getTagValue("verse", eElement));
				    verse.title = new VerseTitle(book, chapter, verseNumber);
				    verse.text = getTagValue("text", eElement);
				    verse.text = verse.text.replace('\n', ' ');
				    for (int i = 0; i < 20; i++)
				    {
				    	verse.text = verse.text.replace("  ", " ");
					}
				   
				    list.add(verse);
				}
			
			}
			
			/*for (int temp = 0; temp < nList.getLength(); temp++) {
	 
			   Node nNode = nList.item(temp);
			   if (nNode.getNodeType() == Node.ELEMENT_NODE) {
	 
			      Element eElement = (Element) nNode;
	 
			      System.out.println("First Name : " + getTagValue("firstname", eElement));
			      System.out.println("Last Name : " + getTagValue("lastname", eElement));
		              System.out.println("Nick Name : " + getTagValue("nickname", eElement));
			      System.out.println("Salary : " + getTagValue("salary", eElement));
	 
			   }
			}*/
			
			for (int i = 0; i < list.size(); i++)
			{
				System.out.println(list.get(i).title);
				System.out.println(list.get(i).text);
			}
			
		  } catch (Exception e) {
			e.printStackTrace();
		  }
	  }
	 
	  private static String getTagValue(String sTag, Element eElement) {
		NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();
	 
	        Node nValue = (Node) nlList.item(0);
	 
		return nValue.getNodeValue();
	  }
}
